<?php
/**
 * all controller extends from me
 *
 * $Id: OA_Controller.php 164 2011-10-15 18:41:46Z alpha1130@gmail.com $
 */

class OA_Controller extends CI_Controller
{
	
	/**
	 * CI_Benchmark
	 * 
	 * @var CI_Benchmark
	 */
	var $benchmark;
	
	/**
	 * CI_Hooks
	 * 
	 * @var CI_Hooks
	 */
	var $hooks;
	
	/**
	 * CI_Config
	 * 
	 * @var CI_Config
	 */
	var $config;
	
	/**
	 * CI_Utf8
	 * 
	 * @var CI_Utf8
	 */
	var $utf8;
	
	/**
	 * CI_URI
	 * 
	 * @var CI_URI
	 */
	var $uri;
	
	/**
	 * CI_Ruoter
	 * 
	 * @var CI_Router
	 */
	var $router;
	
	/**
	 * CI_Output
	 * 
	 * @var CI_Output
	 */
	var $output;
	
	/**
	 * CI_Lang
	 * 
	 * @var CI_Lang
	 */
	var $lang;
	
	/**
	 * CI_Loader
	 * 
	 * @var OA_Loader
	 */
	var $load;
	
	/**
	 * OA_Session
	 * 
	 * @var OA_Session
	 */
	var $session;
	
	/**
	 * CI_Input
	 * 
	 * @var CI_Input
	 */
	var $input;
	
	/**
	 * CI_Security
	 * 
	 * @var CI_Security
	 */
	var $security;
	
	/**
	 * Form_checker
	 * 
	 * @var Form_checker
	 */
	var $form_checker;
	
	/**
	 * 
	 * CI_DB_active_record
	 * 
	 * @var CI_DB_active_record
	 */
	var $db;
	
	function __construct()
	{
		parent::__construct();
		
		$class = $GLOBALS['class'];
		$method = $GLOBALS['method'];
		
		// check user
		if($class !== 'login')
		{
			$status = $this->session->check_acl($class, $method);
			switch($status)
			{
				case OA_Session::ERROR_NOT_LOGIN:
					show_error('帐号未登录，请先<a href="/login">登录</a>');
					break;
				case OA_Session::ERROR_USER_NOT_EXISTS:
					show_error('当前帐号不存在');
					break;
				case OA_Session::ERROR_USER_HAS_BEEN_DELETED:
					show_error('当前帐号已删除');
					break;
				case OA_Session::ERROR_USER_HAS_BEEN_BLOCKED:
					show_error('当前帐号已冻结');
					break;
				case OA_Session::ERROR_PERMISSION_DENIED:
					show_error('你没有访问权限');;
					break;
			}
		}
		
		// register session
		$this->load->vars('SESSION', $this->session);
		
	}
	
	/**
	 * form check
	 * 
	 * @param array $input_form 
	 * 		data check rules, see Form_checker.php
	 * @param mixed $source
	 * 		check data source, default $_GET|$_POST by REQUEST_METHOD
	 * @param array $return_style 
	 * 		return data structure
	 * 		S: is_valid, V: valid_data, R: raw_data, E: error_message
	 * 
	 * @return array
	 * 		check result
	 */
	public function check(array $input_form, $source = NULL, $return_style = 'V') {
		
		if($return_style == '')
		{
			return NULL;
		}
		
		if($source == NULL)
		{
			$_SERVER['REQUEST_METHOD'] == 'POST'
				? $source = &$_POST
				: $source = &$_GET;
		}
		
		$show_error = TRUE;
		$is_valid = $this->form_checker->check($source, $input_form);
		
		$return = array();
		foreach(str_split(strtoupper($return_style)) as $style) {
			switch($style)
			{
				case 'S':
					$return[] = $is_valid;
					break;
				case 'V':
					$return[] = $this->form_checker->get_valid_data();
					break;
				case 'R':
					$return[] = $this->form_checker->get_raw_data();
					break;
				case 'E':
					$return[] = $this->form_checker->get_error_message();
					$show_error = FALSE;
					break;
			}
		}
		
		if(!$is_valid && $show_error)
		{
			show_error(join('<br />', $this->form_checker->get_error_message()));
		}
		
		return $return;
		
	}
	
	/**
	 * response json data for ajax request
	 *
	 * @param mix $data
	 * @param int $code
	 * @param string $msg
	 */
	public function ajax_out($data = NULL, $code = 0, $message = '')
	{
		echo json_encode(array(
			'ret_code' => $code,
			'sys_time' => $this->input->server('REQUEST_TIME'),
			'message' => $message,
			'data' => $data
		));
		exit;
	}
	
}

/* End file of OA_Controller.php */
/* Location: ./application/libraries/OA_Controller.php */
